// pages/bind/bind.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    wxType: wx.getSystemInfoSync().environment ? 0 : 1,
    username: '',
    nameFocus: true,
    password: '',
    passFocus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '登录',
    });
    if (options.mobile){
      this.setData({
        username: options.mobile,
        nameFocus: false,
        passFocus: true
      })
    }
    this.setData({
      userInfo: wx.getStorageSync('wxUserInfo')
    })
  },

  nameInput: function(e) {
    this.setData({
      username: e.detail.value
    })
  },

  passInput: function(e) {
    this.setData({
      password: e.detail.value
    })
  },

  bindSubmit: function(e) {
    this.getFormId(e);
    var that = this;
    if (!this.data.username) {
      app.showConfirm('提示', '请输入登录邮箱或手机', false, function() {
        that.setData({
          nameFocus: true
        })
      })
    } else if (!this.data.password) {
      app.showConfirm('提示', '请输入密码', false, function() {
        that.setData({
          passFocus: true
        })
      })
    } else {
      var data = {
        point_url: app.server.agentApi.bindAccount,
        account: that.data.username,
        password: that.data.password
      }
      app.server.getConnect(data).then(function(res) {
        app.showConfirm('提示', '登录成功', false, function() {
          app.utils.isAuthorize(getApp());
          if(app.globalData.config['tabbarType']==='custom'){
            wx.reLaunch({
              url: app.globalData.config.SwitchTabToAgentPageUrl,
            })
          } else {
            wx.switchTab({
              url: app.globalData.config.SwitchTabToAgentPageUrl,
            })
          }
        }, function (res) {
          app.showConfirm('提示', res.msg, false, function () {
            that.setData({
              passFocus: true,
              password: '',
            })
          })
        })
      })
    }
  },

  // 跳转手机验证
  toMobileVerification: function(e) {
    console.log(e)
    wx.navigateTo({
      url: '../mobileVerification/mobileVerification?pagetype=' + e.currentTarget.dataset.pagetype,
    })
  },

  // 跳转注册
  bindToRegister(){
    let wxType = wx.getSystemInfoSync().environment ? 0 : 1;
    if(wxType){
      wx.redirectTo({
        url: '/pages/agent/center/register/index',
      })
    }else{
      wx.redirectTo({
        url: '../mobileVerification/mobileVerification?pagetype=register'
      })
    }
  },

  // 获取手机号码
  getPhoneNumber(e) {
    if (e.detail.errMsg === 'getPhoneNumber:ok') {
      var that = this;

      // if (wxType) {
      wx.login({
        success: function (res) {
          var data = {
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            code: res.code,
            point_url: app.server.agentApi.getPhoneNumber,
            wxType: that.data.wxType
          };
          app.server.getConnect(data).then(function (res) {
            that.setData({
              ['userInfo.mobile']: res.data['phoneNumber'],
            });
            that.bindRegister();
          })
        }
      })
      // } else {
      //   wx.qy.login({
      //     success: function (e) {
      //       console.log(e)
      //       var data = {
      //         encryptedData: e.detail.encryptedData,
      //         iv: e.detail.iv,
      //         code: res.code,
      //         point_url: app.server.agentApi.getPhoneNumber,
      //         wxType: wxType
      //       };
      //       app.server.getConnect(data).then(function (res) {
      //         that.setData({
      //           ['userInfo.mobile']: res.data['phoneNumber'],
      //         });
      //         that.bindRegister();
      //       })
      //     }
      //   })
      // }
    }
  },

  // 执行注册
  bindRegister() {
    var that = this;
    var data = this.data.userInfo;
    data['point_url'] = app.server.agentApi.register;
    app.server.getConnect(data).then(function (res) {
      that.getUserInfo();
    })
  },

  getUserInfo: function () {
    var that = this;
    var data = {
      point_url: app.server.agentApi.getMemberDetail
    };
    app.server.getConnect(data).then(function (res) {
      wx.setStorageSync("userInfo", res.data);
      app.globalData.userInfo = res.data
      wx.navigateBack();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
